/* @(#)GSHistoryOrderQueryConfirmAction.java
 * 
 *  Project:GS
 *  
 *  Modify  Information:
 *  ===============================================
 *   Author          Date            Description
 *  -------------- ---------------  ---------------
 *  Jenny  Li      2006-5-24          新增
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import system.LoginContext;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.form.GSOrderQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSOrderVO;

/**
 * @author JennyLi
 *
 */
public class GSHistoryOrderQueryConfirmAction extends GSBaseAction {

    /* (non-Javadoc)
     * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
     */
    public String execute(GSBaseForm aForm) throws Exception {
        GSOrderQueryForm tForm = (GSOrderQueryForm)aForm;
        
        //取得页面参数组成订单VO对象
        GSOrderVO tOrderVO = new GSOrderVO();
        tOrderVO.orderid = tForm.orderId;
        tOrderVO.orderDetailId = tForm.orderDetailId;
        tOrderVO.orderDeptId = tForm.orderDeptId;
        tOrderVO.concentricityId = tForm.concentricityId;
        tOrderVO.manufInfo = tForm.manufInfo;
        String[] tDate = new String[2];
        tDate[0] = tForm.orderCreateDateStr;
        tDate[1] = tForm.orderCreateDateEnd;
        tDate = DateUtil.formateDate(tDate);
        tOrderVO.orderCreateDateLowLimie = tDate[0];
        tOrderVO.orderCreateDateUprLimie = tDate[1];
        tOrderVO.orderDetailStatus = GSBaseConstants.ORDER_DELIVERY_CONFIRM;
        tOrderVO.drawingpiececode = tForm.drawingpiececode;
        try{
            //取得当前操作员信息
            LoginContext context = (LoginContext)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            //取得可操作企业及下级企业的订货地址
            
            GSUniteGwProcessor unitePro=new GSUniteGwProcessor();
            aForm.getRequest().setAttribute("orderdept",unitePro.getWareHouseLevelName(context.getLoginWarehouseCode(), context.getLoginWarehouseCode()));
            //取得分页参数
            String[] tStr = this.getPageParams(tForm,tOrderVO);
            //查询订单
            GSOrderProcessor tProc = new GSOrderProcessor();
            ArrayList tList = tProc.getHistoryOrderDetail(tOrderVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
            ResultProcess tRsProc = new ResultProcess();
            tRsProc.setResult(tList);
            tForm.getRequest().setAttribute("order",tRsProc);
            //存储分页参数
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
        }catch(Exception e){
            tForm.getRequest().setAttribute("action","A220301.do");
            throw e;
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }
    /**
     * 取得分页参数
     * @param aForm         基类form
     * @param aOrderVO      订单VO
     * @param aOrg          组织ArrayList对象
     * @return
     * @throws Exception
     */
    private String[] getPageParams(GSBaseForm aForm,GSOrderVO aOrderVO) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        GSOrderDetailDAO tOrderDtlDAO = new GSOrderDetailDAO();
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            totalRecords = tOrderDtlDAO.queryHistoryOrderDetailCount(aOrderVO);
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }

}
